package com.rob.banner.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.UserPreferences;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

@com.google.gwt.gadgets.client.Gadget.ModulePrefs(title = "BannerGadget", author = "Rob Whiteside", author_email = "rob.whiteside@gmail.com")
@com.google.gwt.gadgets.client.Gadget.UseLongManifestName(false)
@com.google.gwt.gadgets.client.Gadget.AllowHtmlQuirksMode(false)

public class BannerGadget extends Gadget<UserPreferences> {

	private int currentImageIndex;
	private ArrayList<HTML> images;
	private ArrayList<Label> labels;
	
	/**
	 * stylesheets are too hard to deal with in gadgets, they don't ever seem to 
	 * refresh when you wanna test changaes, so let's just do it here...
	 */
	private static final String LABEL_STYLE = "  font-size: 2em; " +
			"font-weight: bold; " +
			"color: #FFFFFF; " +
			"background-color: #FFD6FC; " +
			"border: 1px solid white; " +
			"padding: 5px 5px 5px 5px; " +
			"text-align: center;";
	
	private static final String LABEL_STYLE_HIGHLIGHT = LABEL_STYLE + 
			"border: 1px solid 8888FF;" + 
			"background-color: #FFAAF9;";
	
  @Override
  protected void init(UserPreferences preferences) {
		currentImageIndex = -1;
		images = new ArrayList<HTML>();
		labels = new ArrayList<Label>();
		
		ArrayList<String> imageUrls = new ArrayList<String>();
		imageUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/Tip1.png");
		imageUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/Tip2.png");
		imageUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/Tip3.png");
		imageUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/Tip4.png");
		
		ArrayList<String> destUrls = new ArrayList<String>();
		destUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/tip-01");
		destUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/tip-02");
		destUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/tip-03");
		destUrls.add("http://sites.google.com/site/tarasweddingtipsandschedules/tip-04");
		
		ArrayList<String> titles = new ArrayList<String>();
		titles.add("Tip 1");
		titles.add("Tip 2");
		titles.add("Tip 3");
		titles.add("Tip 4");
		
		final Image image1 = new Image(imageUrls.get(0));
		image1.setVisible(false);
		
		AbsolutePanel imagePanel = new AbsolutePanel();
		imagePanel.setWidth("600px");
		imagePanel.setHeight("400px");
		
		HorizontalPanel labelPanel = new HorizontalPanel();
		labelPanel.setWidth("100%");
		
		for (int i = 0; i < imageUrls.size(); i++) {
			HTML html = new HTML("<a href=\"" + destUrls.get(i) + "\" TARGET=\"_parent\"><img src=\"" + imageUrls.get(i) + "\" /></a>");
			imagePanel.add(html, 0, 0);
			html.setVisible(false);
			images.add(html);
			
			Label label = new Label();
			label.getElement().setAttribute("style", LABEL_STYLE);
			label.setText(titles.get(i));
			labels.add(label);
			labelPanel.add(label);
		}
		
		switchToImage(0);
		
		final VerticalPanel vPanel = new VerticalPanel();
		vPanel.add(imagePanel);
		vPanel.add(labelPanel);
		vPanel.add(image1);
		RootPanel.get().add(vPanel);
		
		image1.addLoadHandler(new LoadHandler() {
			
			@Override
			public void onLoad(LoadEvent event) {
				beginRotation();
				vPanel.remove(image1);
			}
		});
	}
	
	private void beginRotation() {
		Timer t = new Timer() {
			@Override
			public void run() {
				if(currentImageIndex+1 >= images.size()) {
					switchToImage(0);
				} else {
					switchToImage(currentImageIndex+1);
				}
			}
		};
		
		t.scheduleRepeating(2000);
	}
	
	private void switchToImage(int imageIndex) {
		GWT.log("Switching to image: " + imageIndex);
		
		Widget oldImage = null;
		Widget newImage = images.get(imageIndex);
		
		labels.get(imageIndex).getElement().setAttribute("style", LABEL_STYLE_HIGHLIGHT);
		if(currentImageIndex != -1) {
			GWT.log("turning off image : " + currentImageIndex);
			oldImage = images.get(currentImageIndex);
			
			labels.get(currentImageIndex).getElement().setAttribute("style", LABEL_STYLE);
		}
		currentImageIndex = imageIndex;

		FadeAnimation fadeAnimation = new FadeAnimation(newImage, oldImage);
		fadeAnimation.run(500);
	}

}
